define(['jquery', 'bootstrap', 'backend', 'form','flow'], function ($, undefined, Backend, Form,Flow) {

    var Controller = {
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            //$("#c-assets_ids").trigger("change");
        },
        show: function () {
            Controller.api.bindevent();
            //$("#c-assets_ids").trigger("change");
            $("#flowchart-show").on("click", function () {
                Fast.api.open('flow/scheme/flowchart?ids=' + Config.lastTask.id + '&flowcode=track&taskid=' + Config.lastTask.id, '流程图', {"area":['950px','600px']});
                return false;
            });
        },
        api: {
            bindevent: function () {
                Flow.bindevents();
                $(".fachoosetable1").on('click', function () {
                    var that = this;
                    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
                    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
                    var apply_unit_id = $(this).data("unit-id") ? $(this).data("unit-id") : '';
                    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
                    var status = $(this).data("status") ? $(this).data("status") : '';
                    var url = $(this).data("url") ? $(this).data("url") : (typeof Backend !==
                    'undefined' ? "auth/adminlog/index" : "");
                    parent.Fast.api.open(url + "?element_id=" + $(this).attr("id") +
                        "&multiple=" + multiple + "&admin_id=" + admin_id +"&apply_unit_id=" + apply_unit_id + "&user_id=" + user_id + "&status=" + status,
                        __('Choose'), {
                            callback: function (data) {
                                var button = $("#" + $(that).attr("id"));
                                var maxcount = $(button).data("maxcount");
                                var input_id = $(button).data("input-id") ? $(button)
                                    .data("input-id") : "";
                                maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
                                if (input_id && data.multiple) {
                                    var idArr = [];
                                    var inputObj = $("#" + input_id);
                                    var value = $.trim(inputObj.val());
                                    if (value !== "") {
                                        idArr.push(inputObj.val());
                                    }
                                    idArr.push(data.id)
                                    var result = idArr.join(",");
                                    if (maxcount > 0) {
                                        var nums = value === '' ? 0 : value.split(/\,/).length;
                                        var files = data.id !== "" ? data.id.toString().split(/\,/) : [];
                                        var remains = maxcount - nums;
                                        if (files.length > remains) {
                                            Toastr.error(__('You can choose up to %d row%s'
                                                , remains));
                                            return false;
                                        }
                                    }
                                    inputObj.val(result).trigger("change").trigger("validate");
                                } else {
                                    $("#" + input_id).val(data.id).trigger("change")
                                        .trigger("validate");
                                }
                            }
                        });
                    return false;
                });

                $(document.body).on("click", "#table .btn-del", function () {
                    let id = $(this).data('id')
                    //var del_id=$("#c-assets_ids").val();
                    var arr = del_id.split(',');

                    for(var i=0;i<arr.length;i++){
                        if(arr[i]==id){
                            arr.splice(i,1);
                            break;
                        }
                    }
                    //$("#c-assets_ids").val(arr.toString());
                    $("#"+id).remove();
                })
            }
        }
    };
    return Controller;
});